package com.love.poetry.interfaces.poetry;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.love.poetry.application.CategoryService;
import com.love.poetry.application.poetry.PoetService;
import com.love.poetry.application.poetry.VerseService;
import com.love.poetry.domain.poetry.Verse;
import com.youboy.core.orm.Page;
import com.youboy.core.web.controller.CrudControllerSupport;
import com.youboy.util.EntityUtils;

@Controller
@RequestMapping(value = "/verse")
public class VerseController extends CrudControllerSupport<String, Verse> {

	@Autowired
	private VerseService verseService;
	@Autowired
	private PoetService poetService;
	@Autowired
	private CategoryService categoryService;
	
	@Override
	protected String getViewPackage() {
		return "/verse";
	}

	private static final String REDIRECT_LIST = "redirect:/verse/list/";

	@Override
	@RequestMapping(value = "/create/", method = RequestMethod.GET)
	public String create(Model model) {
		model.addAttribute(new Verse()).addAttribute("allPoetList", poetService.query(""))
				.addAttribute("threeCategoryList", categoryService.queryCategories());
		return formView();
	}
	@Override
	public String create(Verse entity, BindingResult result) {
		return null;
	}

	@RequestMapping(value = "/create/", method = RequestMethod.POST)
	public String create(Verse entity, BindingResult result,HttpServletRequest request) {
		if (result.hasErrors()) {
			error("verse create error");
			return null;
		}
		entity.setCreateTime(System.currentTimeMillis())
				.setCategory(categoryService.get(request.getParameter("categoryID")))
				.setPoet(poetService.get(request.getParameter("poetID")));
		verseService.save(entity);
		return REDIRECT_LIST;
	}

	@Override
	@RequestMapping(value = "/{id}/delete/", method = RequestMethod.DELETE)
	public String delete(@PathVariable("id") String id) {
		try {
			verseService.delete(id);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return REDIRECT_LIST;
	}

	@Override
	@RequestMapping(value = "/delete/", method = RequestMethod.DELETE)
	public String delete(HttpServletRequest request) {
		String[] items = EntityUtils.nullSafe(
				request.getParameterValues("items"), new String[] {});

		for (String item : items) {
			delete(item);
		}
		return listView();
	}

	@Override
	@RequestMapping(value = "/{id}/edit/", method = RequestMethod.GET)
	public String edit(@PathVariable("id") String id, Model model) {
		Verse verse = verseService.get(id);
		model.addAttribute(verse).addAttribute("_method", "PUT").addAttribute("allPoetList", poetService.query(""))
		.addAttribute("threeCategoryList", categoryService.queryCategories());
		return formView();
	}

	@Override
	@RequestMapping(value = "/{id}/edit/", method = RequestMethod.PUT)
	public String edit(@PathVariable("id") String id, HttpServletRequest request) {
		try {
			Verse entity = verseService.get(id);
			bind(request, entity);
			entity.setCategory(categoryService.get(request.getParameter("categoryID")))
			.setPoet(poetService.get(request.getParameter("poetID")));
			verseService.update(entity);
			success("诗词信息修改成功");
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return REDIRECT_LIST;
	}

	@RequestMapping(value = "/list/", method = RequestMethod.GET)
	public String list(Page<Verse> page, Model model) {
		model.addAttribute(verseService.queryPage(page));
		return listView();
	}
}